﻿using System;
using System.Data.Entity;
using System.Data.Objects;
using System.Reflection;
using System.Transactions;
using MigSharp;
using SnippetBox.Core.Data.Models;

namespace SnippetBox.Core.Data.Context {
    public class DatabaseMigration : IDatabaseInitializer<DatabaseContext> {

        public void InitializeDatabase(DatabaseContext context) {
            bool databaseExists;
            bool useMigration = false;
            
            using (new TransactionScope(TransactionScopeOption.Suppress)) {
                databaseExists = context.Database.Exists();
            }

            if (databaseExists) {
                if (useMigration) {
                    var migrator = new Migrator(context.Database.Connection.ConnectionString, ProviderNames.SqlServer2008);
                    var assembly = Assembly.GetExecutingAssembly();

                    if (!migrator.IsUpToDate(assembly)) {
                        migrator.MigrateAll(assembly);
                    }
                } else {
                    bool throwIfNoMetadata = true;

                    //if (context.Database.CompatibleWithModel(throwIfNoMetadata)) {
                    //    return;
                    //}

                    context.Database.Delete();
                    
                    context.Database.Create();
                    Seed(context);
                    context.SaveChanges();
                }
            } else {
                //context.Database.Create();
                //Seed(context);
                //context.SaveChanges();
            }
        }

        public void Seed(DatabaseContext context) {
            //Password = admin
            var user = new User("codingbug", "5447aed2fdae62d1a4ce8b832beaf865");
            user.Website = "http://codingbug.com";
            user.TwitterUsername = "codingbug";
            user.Role = Role.SuperAdmin;

            var user2 = new User("yoda", "5447aed2fdae62d1a4ce8b832beaf865");
            user2.Website = "http://starwars.com";
            user2.TwitterUsername = "yoda";

            var category1 = new Category("C#");
            var category2 = new Category("ASP.NET", category1);
            var category3 = new Category("ASP.NET MVC", category2);
            var category4 = new Category("ASP.NET WebForms", category2);
            var category5 = new Category("SQL");
            var category6 = new Category("JavaScript");
            var category7 = new Category("HTML");
            var category8 = new Category("CSS");
            var category9 = new Category("BATCH");

            context.Categories.Add(category4);
            context.Categories.Add(category5);
            context.Categories.Add(category6);
            context.Categories.Add(category7);
            context.Categories.Add(category8);
            context.Categories.Add(category9);



            //var snippet1 = new Snippet("My snippet1", "**Hello1** Whats _up_?", category1, user2);
            //var snippet3 = new Snippet("My snippet3", "**Hello3** Whats _up_?", category3, user2);

            //context.Snippets.Add(snippet1);
            //context.Snippets.Add(snippet1);

            //for (int i = 0; i < 300; i++) {
            //    Snippet snippet2 = new Snippet("How to do code snippets in " + i + " seconds", "**Hello3** Whats _up_?", category2, user);
            //    context.Snippets.Add(snippet2);
            //}
            context.Users.Add(user);
            context.Users.Add(user2);

            try {
                context.SaveChanges();
            }
            catch (Exception) {
                var errors = context.GetValidationErrors();

                throw;
            }
        }

    }

    public class FillDatabaseOnModelChanges : DropCreateDatabaseIfModelChanges<DatabaseContext> {
        
        protected override void Seed(DatabaseContext context) {
            
            //Password = admin
            var user = new User("codingbug", "5447aed2fdae62d1a4ce8b832beaf865");
            user.Website = "http://codingbug.com";
            user.TwitterUsername = "codingbug";
            user.Role = Role.SuperAdmin;

            var user2 = new User("yoda", "5447aed2fdae62d1a4ce8b832beaf865");
            user2.Website = "http://starwars.com";
            user2.TwitterUsername = "yoda";

            var category1 = new Category("C#");
            var category2 = new Category("ASP.NET", category1);
            var category3 = new Category("ASP.NET MVC", category2);
            var category4 = new Category("ASP.NET WebForms", category2);
            var category5 = new Category("SQL");
            var category6 = new Category("JavaScript");
            var category7 = new Category("HTML");
            var category8 = new Category("CSS");
            var category9 = new Category("BATCH");

            context.Categories.Add(category4);
            context.Categories.Add(category5);
            context.Categories.Add(category6);
            context.Categories.Add(category7);
            context.Categories.Add(category8);
            context.Categories.Add(category9);



            var snippet1 = new Snippet("My Snippet1", "**Hello1** Whats _up_?", category1, user);
            var snippet3 = new Snippet("My 3", "**Hello3** Whats _up_?", category3, user2);

            context.Snippets.Add(snippet1);
            context.Snippets.Add(snippet3);
            
            //for (int i = 0; i < 300; i++) {
            //    Snippet snippet2 = new Snippet("How to do code snippets in " + i + " seconds", "**Hello3** Whats _up_?", category2, user);
            //    context.Snippets.Add(snippet2);
            //}
            context.Users.Add(user2);

            try {
                context.SaveChanges();            
            }
            catch (Exception) {
                var errors = context.GetValidationErrors();
                
                throw;
            }
            
        } 
    }
}